package com.example.store.filter;

import com.example.store.common.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "loginCheckFilter",urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {

    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;

        String requestURI = request.getRequestURI();
        String[] urls = new String[]{
                "/front/login.html",
                "/front/js/**",
                "/front/css/**",
                "/user/login",
                "/user/guest",

                "/seller/login",
                "/backend/js/**",
                "/backend/css/**",
                "/backend/login.html",
                "/backend/adminLogin.html",
                "/seller/adminLogin"
        }; //直接放进来的

        boolean check = check(urls, requestURI);

        if(check){
            filterChain.doFilter(request,response);
            return;
        }

        String[] back = new String[]{
            "/backend/**"
        };//后台的重要资源

        String[] admin = new String[]{
                "/backend/adminMain**"
        };//管理员专属

        String userId = (String) request.getSession().getAttribute("user");
        if(userId != null){
            BaseContext.setCurrentId(userId);

            if(check(admin, requestURI)){//管理员专属
                if(!userId.equals("root")){
                    return;
                }
            }

            if(check(back, requestURI)){//销售人员或管理员可用
                if(userId.charAt(0)!='s'&&!userId.equals("root")){
                    return;
                }
            }

            filterChain.doFilter(request,response);
        }
        else{//不同情况下的跳转
            if(check(admin, requestURI)){
                ((HttpServletResponse) servletResponse).sendRedirect("/backend/adminLogin.html");
            }
            else if(check(back, requestURI)){
                ((HttpServletResponse) servletResponse).sendRedirect("/backend/login.html");
            }
            else{
                ((HttpServletResponse) servletResponse).sendRedirect("/front/login.html");
            }
        }
    }

    @Override
    public void destroy() {

    }

    public boolean check(String[] urls, String requestURI){
        for(String url : urls){
            boolean match = PATH_MATCHER.match(url, requestURI);
            if(match){
                return true;
            }
        }
        return false;
    }
}
